package com.kangyi.user.controller;

import com.kangyi.dto.NursingHomeReviewDTO;
import com.kangyi.dto.ReviewPageQueryDTO;
import com.kangyi.result.PageResult;
import com.kangyi.result.Result;
import com.kangyi.user.service.NursingHomeReviewService;
import com.kangyi.vo.NursingHomeReviewListVO;
import com.kangyi.vo.NursingHomeReviewVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/user/review")
@Slf4j
@Api(tags = "评论相关接口")
public class NursingHomeReviewController {

    @Autowired
    private NursingHomeReviewService nursingHomeReviewService;


    @GetMapping("/list/{userId}")
    @ApiOperation("查询当前用户下发表的评论")
    @Cacheable( cacheNames = "review_", key = "'userList_'+#userId")
    public Result<List<NursingHomeReviewListVO>> listReview(@PathVariable Integer userId){
        log.info("查询当前用户下发表的评论:{}",userId);
        try {
            List<NursingHomeReviewListVO> nursingHomeReviewVOList = nursingHomeReviewService.listReview(userId);
            return  Result.success(nursingHomeReviewVOList,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除评论")
    @CacheEvict( cacheNames = "review_", allEntries = true)
    public Result deleteReview(@PathVariable Integer id){
        log.info("删除评论：{}",id);
        try {
            nursingHomeReviewService.deleteReview(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    @PostMapping("/release")
    @ApiOperation("发表评论")
    @CacheEvict(cacheNames = "review_" ,allEntries = true)
    public Result releaseReview(@RequestBody NursingHomeReviewDTO nursingHomeReviewDTO){
        log.info("发表评论：{}",nursingHomeReviewDTO);
        try {
            nursingHomeReviewService.releaseReview(nursingHomeReviewDTO);
            return Result.success("发表成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/page")
    @ApiOperation("评论区分页查询")
    public Result<PageResult> pageReview(ReviewPageQueryDTO reviewPageQueryDTO){
        log.info("评论区分页查询:{}",reviewPageQueryDTO);
        try {
            PageResult pageResult = nursingHomeReviewService.pageReview(reviewPageQueryDTO);
            return Result.success(pageResult,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
